#pragma once
#include <openssl/sha.h> // 需要安装OpenSSL库
#include <string>
#include <vector>
#include <cctype>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <functional>

#ifdef _WIN32
    #include <windows.h>
    #include <iphlpapi.h>
    #include <intrin.h>
    #pragma comment(lib, "iphlpapi.lib")
#elif defined(__linux__)
    #include <fstream>
    #include <dirent.h>
    #include <cstring>
    #include <unistd.h>
    #include <sys/ioctl.h>
    #include <net/if.h>
    #include <sys/utsname.h>
    #include <fcntl.h>
#endif

namespace tools {
static long long Umax = 56800235583LL;
static std::string to62sHash = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
// 转62进制
std::string to62s(long long x) {
    std::string ret;
    while(x) {
        ret += to62sHash[x % 62];
        x /= 62; 
    }
    while(ret.size() < 6) ret += '0';
    return ret;
}
// 获取主板UUID
std::string getMotherboardUUID() {
    std::string uuid;

#ifdef _WIN32
    // Windows实现
    HKEY hKey;
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
                    "HARDWARE\\DESCRIPTION\\System\\BIOS", 
                    0, KEY_READ, &hKey) == ERROR_SUCCESS) {
        char buffer[128];
        DWORD bufferSize = sizeof(buffer);
        DWORD type;
        
        if (RegQueryValueEx(hKey, "SystemProductName", NULL, &type, 
                           (LPBYTE)buffer, &bufferSize) == ERROR_SUCCESS) {
            uuid = buffer;
        }
        
        if (uuid.empty() && 
            RegQueryValueEx(hKey, "BaseBoardProduct", NULL, &type, 
                           (LPBYTE)buffer, &bufferSize) == ERROR_SUCCESS) {
            uuid = buffer;
        }
        
        RegCloseKey(hKey);
    }
#elif defined(__linux__)
    // Linux实现
    std::ifstream file("/sys/devices/virtual/dmi/id/board_serial");
    if (file.is_open()) {
        std::getline(file, uuid);
        file.close();
    }
    
    if (uuid.empty()) {
        file.open("/sys/devices/virtual/dmi/id/product_uuid");
        if (file.is_open()) {
            std::getline(file, uuid);
            file.close();
        }
    }
    
    if (uuid.empty()) {
        file.open("/sys/devices/virtual/dmi/id/product_serial");
        if (file.is_open()) {
            std::getline(file, uuid);
            file.close();
        }
    }
#endif

    // 清理UUID
    uuid.erase(std::remove_if(uuid.begin(), uuid.end(), 
              [](char c) { return !std::isalnum(c); }), uuid.end());
    return uuid;
}
}